package com.lcredu.springboot.furn.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lcredu.springboot.furn.entity.Furn;
import com.lcredu.springboot.furn.service.FurnService;
import com.lcredu.springboot.furn.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * @author 黎长容
 * @date 2025-08-19 18:45
 */
@RestController
@Slf4j
public class FurnController {
    @Resource
    private FurnService furnService;

    //添加Furn
    @PostMapping("/save")
    public Result save(@Validated @RequestBody Furn furn, Errors errors) {
        //数据校验
        HashMap<String, Object> filedErrorMap = new HashMap<>();
        for (FieldError fieldError : errors.getFieldErrors()) {
            filedErrorMap.put(fieldError.getField(), fieldError.getDefaultMessage());
        }

        if (filedErrorMap.isEmpty()) {
            //没有错误
            furnService.save(furn);
            return Result.success();
        } else {
            return Result.error("400", "验证不通过", filedErrorMap);
        }


    }

    //展示所有的数据
    @RequestMapping("/furnList")
    public Result getFurnList() {
        List<Furn> furnList = furnService.list();
        return Result.success(furnList);
    }

    @GetMapping("/getFurn")
    public Result getFurn(Integer id) {
        log.info("id=" + id);
        Furn furn = furnService.getById(id);
        return Result.success(furn);
    }


    @PostMapping("/updateFurn")
    public Result updateFurnById(@RequestBody Furn furn) {
        log.info("接收到的furn={}", furn);
        if (furnService.updateById(furn)) {
            return Result.success();
        } else {
            return Result.error("500", "修改错误");
        }
    }


    //删除家居
    @PostMapping("/deleteFurn/{id}")
    public Result deleteFurn(@PathVariable("id") Integer id) {
        log.info("接收到的id={}", id);
        if (furnService.removeById(id)) {
            return Result.success();
        } else {
            return Result.error("500", "删除错误");
        }
    }


    //分页查询
    @GetMapping("/furnPage")
    public Result furnListByPage(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                 @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize) {
        //调用?
        Page<Furn> page = furnService.page(new Page<>(pageNum, pageSize));
        return Result.success(page);
    }

    //根据条件进行查询
    @GetMapping("/furnPageBySearch")
    public Result furnListByPageAndSearch(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                          @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize,
                                          @RequestParam(value = "search", required = false, defaultValue = "") String search) {
        QueryWrapper<Furn> queryWrapper = Wrappers.query();
        if (StringUtils.hasText(search)) {
            queryWrapper.like("name", search);
        }
        Page<Furn> page = furnService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return Result.success(page);
    }


    @GetMapping("/furnPageBySearch2")
    public Result furnListByPageAndSearch2(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                           @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize,
                                           @RequestParam(value = "search", required = false, defaultValue = "") String search) {
        LambdaQueryWrapper<Furn> lambdaQueryWrapper = Wrappers.lambdaQuery();
        if (StringUtils.hasText(search)) {
            lambdaQueryWrapper.like(Furn::getName, search);
        }

        Page<Furn> page = furnService.page(new Page<>(pageNum, pageSize), lambdaQueryWrapper);
        return Result.success(page);
    }

}
